﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.Utils;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PR_IncomeOnMonth : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IEmployeeTypeBLL empTypeBLL = new EmployeeTypeBLL();
        private IPayrollsBLL payrollsBLL = new PayrollsBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            IncludeJava();

            if (!IsPostBack)
            {
                bindEmpType();
                bindYear();
            }

        }
        public void IncludeJava()
        {
            HtmlGenericControl script = new HtmlGenericControl();
            script.TagName = "script";
            script.Attributes.Add("type", "text/javascript");
            script.Attributes.Add("src", "../../Scripts/jsfunction.js");
            Page.Header.Controls.Add(script);
        }
        private void bindYear()
        {
            DataTable dt = new DataTable();
            DataColumn dc = dt.Columns.Add("Name");

            for (int i = 2500; i <= 2600; i++)
            {
                dt.Rows.Add(Convert.ToString(i));
            }
            
            cmbFilterYear.DataSource = dt;
            cmbFilterYear.DataBind();
            cmbFilterYear.SelectedValue = Convert.ToString(Int32.Parse(DateTime.Today.Year.ToString()) + 543);
        }

        private void LoadDataGrid()
        {
            try
            {
                DataSet incomeTypeEmp = payrollsBLL.ListPayrollsEmpTypeDate(int.Parse(ddlEmployeeType.SelectedValue), int.Parse(ddlMonth.SelectedValue), int.Parse(cmbFilterYear.SelectedValue));
                
                RadGrid2.DataSource = incomeTypeEmp.Tables[0];
                RadGrid2.DataBind();
            }
            catch (Exception ex)
            {
                Web.ShowAlert(this, ex.Message);
            }
        }

        private void bindEmpType()
        {
            try
            {
                IList<EmployeeTypeInfo> infoList = empTypeBLL.ListEmployeeTypes();
                ddlEmployeeType.DataSource = infoList;
                ddlEmployeeType.DataBind();
            }
            catch
            {

            }
        }

        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "Delete")
                {
                    int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["pay_dtls_seq"]);

                    payrollsBLL.DeleteEmpPayroll(seq, ssMng.EmpSeq);
                }
                else if (e.CommandName == "btnClick")
                {
                    return;
                }
                else if (e.CommandName == "InsertSelect")// Auto Insert
                {
                    int nEmpTypeSeq = Int32.Parse(ddlEmployeeType.SelectedValue);
                    int nBgYear = Int32.Parse(cmbFilterYear.SelectedValue);
                    int nMonth = Int32.Parse(ddlMonth.SelectedValue);
                    string strUser = ssMng.EmpSeq;
                    int nPayrollSeq = 0;
                    DataSet ds = new DataSet();

                    bool dup = payrollsBLL.ChkDuplicatePayrolls(nEmpTypeSeq, nBgYear, nMonth);
                    if (dup != true)
                    {
                        payrollsBLL.InsertPayrolls(nEmpTypeSeq, nBgYear, nMonth, strUser);
                    }

                    ds = payrollsBLL.ListPayrolls(nEmpTypeSeq, nBgYear, nMonth);
                    if (ds.Tables.Count > 0)
                        nPayrollSeq = int.Parse(ds.Tables[0].Rows[0]["pay_seq"].ToString());

                    payrollsBLL.InsertSelect(nEmpTypeSeq, nBgYear, nMonth, nPayrollSeq, strUser);
                }

                LoadDataGrid();
            }
            catch (Exception ex)
            {
                Web.ShowAlert(this, ex.Message);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Filter();
        }
        private void Filter()
        {
            LoadDataGrid();
            Session["et|m|y"] = ddlEmployeeType.SelectedValue + "|" + ddlMonth.SelectedValue + "|" + cmbFilterYear.SelectedValue;
        }
        protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {

        }
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            Filter();
        }
        protected void ddlEmployeeType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Session["EMP_TYPE_SEQ"] = ddlEmployeeType.SelectedValue;
            Filter();
        }

        protected void ddlMonth_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Filter();
        }

        protected void cmbYear_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Filter();
        }

    }
}
